package com.itheima.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.mapper.ClazzMapper;
import com.itheima.pojo.Clazz;
import com.itheima.pojo.ClazzQueryParam;
import com.itheima.pojo.StudentQueryParam;
import com.itheima.pojo.PageResult;
import com.itheima.service.ClazzService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class ClazzServiceImpl implements ClazzService {

    @Autowired
    private ClazzMapper clazzMapper;

    //添加班级信息
    @Override
    public void add(Clazz clazz) {
        //1.补全基本信息
        clazz.setCreateTime(LocalDateTime.now());
        clazz.setUpdateTime(LocalDateTime.now());

        //2.调用dao层保存数据
        clazzMapper.insert(clazz);

    }

    //查询所有班级信息
    @Override
    public List<Clazz> list() {
        return clazzMapper.list();
    }

    //班级列表查询
    @Override
    public PageResult<Clazz> page(ClazzQueryParam clazzQueryParam) {
        //1.设置分页参数
        PageHelper.startPage(clazzQueryParam.getPage(), clazzQueryParam.getPageSize());

        //2.执行查询
        List<Clazz> clazzList = clazzMapper.Page(clazzQueryParam);
        //状态  - 当前时间 < 开课时间：未开课
    //  - 当前时间 > 结课时间：已结课
    //  - 否则：在读
        //0.未开课 1.在读 2.已结课
        //Integer.valueOf是将字符串转换为整数
//        for (Clazz clazz : clazzList) {
//            if (LocalDateTime.now().isBefore(clazz.getBeginDate().atStartOfDay())) {
//                clazz.setStatus("未开课");
//            } else if (LocalDateTime.now().isAfter(clazz.getEndDate().atStartOfDay())) {
//                clazz.setStatus("已结课");
//            } else {
//                clazz.setStatus("在读");
//            }
//        }

        //3.封装分页结果
        Page<Clazz> page = (Page<Clazz>) clazzList;
        return new PageResult<>(page.getTotal(), page.getResult());
        //page.getResult()获取当前页的结果集是一个List即page
    }

    //删除班级
    @Override
    public void delete(Long id) {
        clazzMapper.delete(id);

    }

    //根据id查询班级

    @Override
    public Clazz get(Long id) {
        return clazzMapper.get(id);
    }

    //修改班级
    @Override
    public void update(Clazz clazz) {
        //1.补全基本信息
        clazz.setUpdateTime(LocalDateTime.now());

        //2.调用dao层保存数据
        clazzMapper.update(clazz);
    }
}
